<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #content{ 
            height: 100px;
            width: 200px;
            border:1px solid red
        }
    </style>
</head>
<body>
    <div id="content">姓名：</div>

    <script>
        const content = document.getElementById('content');
        window.onkeydown = function(){
            const xhr = new XMLHttpRequest();
            //2.对传入的json数据自动进行转换
            xhr.responseType = 'json';
            xhr.open('GET','http://127.0.0.1:8085/json-server');
            xhr.send();
            xhr.onreadystatechange = function(){
                if(xhr.readyState == 4){
                    if(xhr.status >= 200 && xhr.status < 300){
                        // content.innerHTML = xhr.response;
                        //1.对传入的json数据手动对进行转换
                        // const data = JSON.parse(xhr.response);
                        // content.innerHTML += data.name;

                        //自动转换
                        content.innerHTML += xhr.response.name
                    }
                }
            }
        }
    </script>
</body>
</html>